package com.mmall.listener;

import com.google.common.collect.Lists;
import com.mmall.annotation.RequsetPremission;
import com.mmall.common.ApplicationContextHelper;
import com.mmall.dto.RequestPremissionDto;
import com.mmall.service.SysCoreService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;

import static com.mmall.util.ClassUtil.getClasses;

@Component("MyListenerProcessor")
@Slf4j
public class MyListenerProcessor implements BeanPostProcessor {
    private List<RequestPremissionDto> requsetPremissions = Lists.newLinkedList();
    private int NUM = 0;
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        SysCoreService sysCoreService = ApplicationContextHelper.popBean(SysCoreService.class);
        Annotation permissionOperationClass = bean.getClass().getDeclaredAnnotation(RequsetPremission.class);
        //类上必须要有注解，不然权限将不会进行写入
        if (permissionOperationClass != null) {
            //判断权限是否生效
            if (((RequsetPremission) permissionOperationClass).state()) {
                //获取方法组
                Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
                if (methods != null) {
                    List<RequestPremissionDto> premissionDtos = Lists.newLinkedList();
                    for (Method method : methods) {
                        RequsetPremission permissionOperation = AnnotationUtils.findAnnotation(method, RequsetPremission.class);
                        if (null != permissionOperation) {
                            if (permissionOperation.state()) {
                                //插入到数据中
                                RequestPremissionDto dto1 = RequestPremissionDto.builder()
                                        .description(permissionOperation.permissionName())
                                        .url(((RequsetPremission) permissionOperationClass).url()+permissionOperation.url())
                                        .build();
                                premissionDtos.add(dto1);
                            }
                        }
                    }
                    RequestPremissionDto dto = RequestPremissionDto.builder().url(((RequsetPremission) permissionOperationClass).url())
                            .description(((RequsetPremission) permissionOperationClass).permissionName()).dtos(premissionDtos).build();
                    requsetPremissions.add(dto);
                }
            }
        }
        //如果bean被注入后
        if (sysCoreService !=null){
            NUM++;
            if (NUM>0 && NUM < 2) {
                log.info("\n-------------------------------------权限校验----------------------------------");
                sysCoreService.invalidAcl(requsetPremissions);
            }
        }
        return bean;

    }

}
